//index.js
//获取应用实例
const app = getApp();
const urlConfig = require('../../config/urlConfig.js');
var recorderManager;
Page({
    data: {
        voiceInfo: '加速后链接',
        commandInput: '',
        voiceInfoPath: '',
        groupInput: 111
    },
    onLoad: function() {
        var that = this;
        recorderManager = wx.getRecorderManager();
        recorderManager.onStop(function(e) {
            console.log(e);
            var voiceFilePath = e.tempFilePath;
            that.setData({
                voiceInfo: "上传文件",
                voiceInfoPath: voiceFilePath
            });
            if (!that.data.groupInput) {
                wx.showToast({
                    title: '请先输入分组',
                    icon: 'none'
                });
                return;
            }
            wx.uploadFile({
                url: urlConfig.audioUrl,
                filePath: voiceFilePath,
                formData: {
                    sid: that.data.groupInput
                },
                name: 'file',
                success: function(result) {
                    that.setData({
                        voiceInfo: "上传成功"
                    });
                    var data = JSON.parse(result.data);
                    that.setData({
                        voiceInfo: "请求结果：" + result.data
                    });
                }
            });
        });
    },
    bindTouchStart: function(e) {
        this.setData({
            voiceInfo: "开始录音"
        });
        recorderManager.start({
            format: "mp3",
            duration: 10000
        });
    },
    bindTouchEnd: function() {
        this.setData({
            voiceInfo: "结束录音"
        });
        recorderManager.stop();
    },
    bindCommandInput: function(e) {
        this.data.commandInput = e.detail.value;
    },
    bindGroupInput: function(e) {
        this.data.groupInput = e.detail.value;
    },
    sendCommand: function() {
        var that = this;
        if (!that.data.groupInput) {
            wx.showToast({
                title: '请先输入分组',
                icon: 'none'
            });
            return;
        }
        if (!that.data.commandInput) {
            wx.showToast({
                title: '请先输入命令内容',
                icon: 'none'
            });
            return;
        }
        wx.request({
            url: urlConfig.callUrl,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                sid: that.data.groupInput,
                cmd: this.data.commandInput
            },
            method: "POST",
            success: function(result) {
                that.setData({
                    voiceInfo: "请求结果：" + JSON.stringify(result.data)
                });
            }
        });
    }
})